<!--
* description: 无联动下拉框-调API取值
* author: tfy
* createdDate: 2019-10-11
-->
<template>
  <section>
    <el-col :span="span">
      <div class="comwidth">
        <lableName
          :validrule="validrule"
          :curLabelName="curLabelName"
          :isShowLabel="isShowLabel"
          :isRequire="isRequire"
        ></lableName>

        <el-select
          :multiple="isMul"
          collapse-tags
          :filterable="filterable"
          v-model="modelCode"
          placeholder="请选择"
          @clear="clearCodeSelect"
          :clearable="clearable"
          @change="sendCode1"
          :disabled="disabled"
          :readonly="readonly"
          size="small"
        >
          <el-option v-if="chackAll" :label="'所有'+curLabelName" value="all"></el-option>
          <el-option
            v-for="item in optionDatas"
            :key="item[codeField]"
            :label="item[textField]"
            :value="item[codeField]"
          >{{item[textField]}}</el-option>
        </el-select>
        <validateFormate :curLabelName="curLabelName" :validrule="validrule"></validateFormate>
      </div>
    </el-col>
  </section>
</template>
<script>
import { crmApis } from "@/api/apiList/crm";
import { requestAPI } from "@/api/commonRequest";
// import lableName from "@/components/lableName";
import { dropdownMixins } from "@/components/mixins/dropdownMixins";
// import validateFormate from "@/components/validateFormate";
export default {
  name: "singleDropList",
  // 组件混入对象：{props[span,isShowLabel,code,isMul,labelName] data[modelCode,optionDatas,listQuery] methods[queryTable,sendCode]}
  mixins: [dropdownMixins],
  components: {
    lableName: () => import("@/components/lableName"), //标题溢出样式组件
    validateFormate: () => import("@/components/validateFormate"), //校验提示样式组件
  },
  props: {
    // 值列表类型编码
    lookuptype: { type: Object, default: {} },
    // 获取组件中的名称，如：textField="carBrandCode"
    codeField: { type: String, default: "lookupValueCode" },
    // 获取组件中的名称，如：textField="carBrandName"
    textField: { type: String, default: "lookupValueName" },
    extralParams: {
      type: Object,
      default: function () {
        return {};
      },
    },
    chackAll: {
      type: Boolean,
      default: function () {
        return false;
      },
    },
  },
  data() {
    return {
      // API配置对象
      apiConfig: this.lookuptype,
      // 表格中台返回网格的字段
      apiQueryRow: ["oemCode", "groupCode", this.codeField, this.textField],
      // 下拉查询条件
      listQuery: this.getListQuery(),
      allModelCode: false,
    };
  },
  watch: {
    modelCode(val, oldval) {
      if (this.chackAll) {
        let newindex = val.indexOf("all"),
          oldindex = oldval.indexOf("all"); //获取val和oldval里all的索引,如果没有则返回-1
        if (newindex != -1 && oldindex == -1) {
          if (this.optionDatas.length === 0) {
            this.modelCode = "";
            return;
          }
          //如果新的选择里有勾选了选择所有 则 勾选所有选项
          this.allModelCode = true;
          this.modelCode = ["all"];
          for (var i in this.optionDatas) {
            this.modelCode.push(this.optionDatas[i][this.codeField]);
          }
        } else if (newindex == -1 && oldindex != -1) {
          //取消勾选所以  则移除掉所有项的勾选
          this.allModelCode = false;
          this.modelCode = [];
        }
        if (val.length < oldval.length && newindex != -1 && this.allModelCode) {
          this.allModelCode = false;
          // this.modelCode.splice(this.modelCode.indexOf("all"), 1);
        }
        if (
          val.length == this.optionDatas.length &&
          !this.allModelCode &&
          this.modelCode.length > 0
        ) {
          this.allModelCode = true;
          this.modelCode = ["all"].concat(this.modelCode);
        }
      }
      let str = this.codeField;
      if (str) {
        let queryVal = val.indexOf("all") == -1 ? val : "";
        this.$emit(
          "changeDropValue",
          queryVal,
          "",
          this.codeField,
          this.textField
        );
        this.$emit(
          "changeValuewithSame",
          queryVal,
          "",
          this.codeField,
          this.textField
        );
      }
      this.sendCode();
    },
  },
  mounted() {
    if (this.code != "" && this.code != null) {
      this.$emit(
        "changeDropValue",
        this.code,
        this.text,
        this.codeField,
        this.textField
      );
    }
  },
  methods: {
    getListQuery() {
      if (this.extralParams) {
        return Object.assign({ isEnable: "1" }, this.extralParams);
      } else {
        return { isEnable: "1" };
      }
    },
    sendCode1(value) {
      let that = this;
      let val = this.modelCode;
      let text = this.codeField;
      let j = "";
      let option = that.optionDatas;
      if (typeof value == "string") {
        j = value;
        var name = "";
        for (var i = 0; i < option.length; i++) {
          if (option[i][that.codeField] == j) {
            name = option[i][that.textField];
            this.$emit(
              "changeDropValue",
              j,
              name,
              that.codeField,
              that.textField
            );
          }
        }
      } else {
        j = value.join(",");
        this.$emit("changeDropValue", j, name, that.codeField, that.textField);
      }

      this.sendCode();
      this.$emit("changeCode", val, text, that.textField, name);
      this.$emit("sendChangeCode", val, text, that.textField, name);
      this.$emit("getCode", val, text, that.textField, name);
    },
    clearCodeSelect() {
      this.sendCode1();
    },
  },
};
</script>
